package cn.com.surker.workcon.service.siwei;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.enums.production.indent.ProTypeEnum;
import cn.com.surker.enums.production.invoice.InvoiceProcessEnum;
import cn.com.surker.enums.production.invoice.InvoiceSendTypeEnum;
import cn.com.surker.lab.dao.LabExpendUploadDao;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.production.dao.ProductionIndentDao;
import cn.com.surker.production.entity.ProductionIndentDispatch;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ProduceService;
import cn.com.surker.workcon.service.bcs.common.ProduceEnum;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 上海思维派单
 *
 * @author luotao
 * @date 2024/12/18
 */
public class ProduceServiceImpl extends GKJDBCService implements ProduceService {

	@Resource
	private LabExpendUploadDao labUploadDao;

	@Resource
	private ProductionIndentDao indentDao;

	public Integer queryByDispatchId(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return null;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return null;
		}
		//	改为派单表中存在数据就是等待接收,没存在就是接收了
		//	在生产了
		String selectSql = "select top 1 FNo,FZt from TJlb where FPcbID = ?";
		Object[] params = {dispatchId};
		//	查询数据
		List<Map<String, Object>> maps = lineTemplate.queryForList(selectSql, params);
		if (maps == null || maps.isEmpty()) {
			// 没有接收,返回等待接收状态
			/*ProductionIndentDispatch erpDisPatch = new ProductionIndentDispatch();
			erpDisPatch.setProStatus(InvoiceProcessEnum.QUERN_RECEIVE.getCode());*/
			return InvoiceProcessEnum.QUERN_RECEIVE.getCode();
		}
		//	有数据表示就被接收了
		/*ProductionIndentDispatch erpDisPatch = new ProductionIndentDispatch();
		erpDisPatch.setProStatus(InvoiceProcessEnum.QUERN_PRO.getCode());*/
		return InvoiceProcessEnum.QUERN_PRO.getCode();
	}

	@Override
	public Boolean disHasDel(Long lineId, Long dispatchId) {
		//	TODO
		return null;
	}

	@Override
	public int insert(Long lineId, ProductionIndentDispatch dispatch) {
		if (lineId == null || dispatch == null) {
			return -1;
		}
		//	任务单id
		Long indentId = dispatch.getIndentId();
		//	查询任务单数据
		ProductionIndentVo indentVo = indentDao.queryFullById("CX_ZPBJN", indentId);
		if (indentVo == null) {
			System.err.println("未查询出正确的任务单信息," + indentId);
			return -1;
		}
		List<LabExpendUpload> labExpendUploads = labUploadDao.queryByIndentId("CX_ZPBJN",indentVo.getOrgId(), indentId, lineId);
		if (labExpendUploads == null || labExpendUploads.isEmpty()) {
			System.err.println("派单未查询出配合比数据");
			return -1;
		}
		String labLevelCode = null;
		for (LabExpendUpload expendUpload : labExpendUploads) {
			Long uploadLine = expendUpload.getUploadLine();
			if (lineId.equals(uploadLine)) {
				labLevelCode = expendUpload.getCode();
			}
		}
		if (StringUtils.isBlank(labLevelCode)) {
			System.err.println("派单未匹配出" + lineId + "线的配合比数据");
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	任务单编号
		Long FRwdID = indentId;
		//	派单版本号
		int FRwdVer = 0;
		//	合同编号
		String FHtno = indentVo.getConCode();
		//	客户名称
		String FHtdw = indentVo.getClientName();
		//	工程名称
		String FGcmc = indentVo.getConName();
		//	施工部位
		String FJzbw = indentVo.getSite();
		//	浇筑方式
		String FJzfs = indentVo.getCastModelName();
		//	工程地址
		String FGcdz = indentVo.getAddress();
		//	运距
		Double FGls = indentVo.getDistance();
		//	砼类别,例: 混凝土＝大写'C', 砂浆＝大写'M'，空＝混凝土
		String FTlb = "C";
		Integer uploadType = dispatch.getUploadType();
		if (InvoiceSendTypeEnum.CONCRETE.getCode().equals(uploadType)) {
			FTlb = "C";
		} else if (InvoiceSendTypeEnum.SAND.getCode().equals(uploadType)) {
			FTlb = "M";
		}
		//	砼强度
		String FTpz = indentVo.getStrengthName();
		//	塌落度
		String FTld = indentVo.getSlumps();
		// 生产拌台,例："1","2"
		String FScbt = "1";
		//	生产车号
		String FShch = dispatch.getCarCode();
		// 司机姓名
		String FSjxm = dispatch.getDriver();
		// 本车方数
		Double FBcfs = dispatch.getUploadStere();
		//	派车序号
		String FPcno = dispatch.getTotalCar().toString();
		//	派车时间
		String FPcsj = DateUtil.convertString(new Date());
		//	出货时间
		String FChsj = FPcsj;
		//	任务单状态
		String FZt = ProTypeEnum.WORK.getDescription();
		// 备用字段3
		String FC3 = "1";
		//	搅拌时间
		String FJbsjS = "";
		// 配合比编号
		String FPhbID = labLevelCode;
		// ?
		String FPhbVer = "0";
		//	备用字段1
		String FA1 = "";
		//	派车id
		Long FPcbID = dispatch.getId();
		//	插入sql
		String sql = "INSERT INTO VTglPcbSync_T (FRwdID,FRwdVer,FHtno,FHtdw,FGcmc,FJzbw,FJzfs,FGcdz,FGls,FTlb,FTpz,FTld ,FScbt,FShch,FSjxm,FBcfs,FPcno,FPcsj,FChsj,FZt,FC3,FJbsjS,FPhbID,FPhbVer,FA1,FPcbID)\n" +
			"Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		Object[] params = {FRwdID, FRwdVer, FHtno, FHtdw, FGcmc, FJzbw, FJzfs, FGcdz, FGls, FTlb, FTpz, FTld, FScbt, FShch, FSjxm, FBcfs, FPcno, FPcsj, FChsj, FZt, FC3, FJbsjS, FPhbID, FPhbVer, FA1, FPcbID};
		//	开启事务
		beginTransaction(lineId);
		//	插入数据
		int insert = lineTemplate.update(sql, params);
		return insert;
	}

	@Override
	public Integer delDispatch(Long lineId, Long dispatchId) {
		if (lineId == null || dispatchId == null) {
			return -1;
		}
		SysLine line = getLine(lineId);
		JdbcTemplate lineTemplate = getLineJdbcTemp(line);
		//	没有数据源
		if (lineTemplate == null) {
			return -1;
		}
		//	删除sql
		String delSql = "delete from VTglPcbSync_T where FPcbID = ?";
		Object[] params = {dispatchId};
		//	开启事务
		beginTransaction(lineId);
		int update = lineTemplate.update(delSql, params);
		return update;
	}
}


